package com.heima.kafka.chapter1;

/**
 * Kafka 消息生产者
 见代码库 com.heima.kafka.chapter1.ConsumerFastStart
 */
public class ProducerFastStart {
    // Kafka集群地址
    private static final String brokerList = "10.3.9.161:9092,10.3.9.162:9092,10.3.9.165:9092";
    // 主题名称-之前已经创建
    private static final String topic = "heima";
    public static void main(String[] args) {
        Properties properties = new Properties();
// 设置key序列化器
        properties.put("key.serializer",
                "org.apache.kafka.common.serialization.StringSerializer");
//另外一种写法
        properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,
        StringSerializer.class.getName());
// 设置重试次数
        properties.put(ProducerConfig.RETRIES_CONFIG, 10);
// 设置值序列化器
        properties.put("value.serializer",
                "org.apache.kafka.common.serialization.StringSerializer");
// 设置集群地址
        properties.put("bootstrap.servers", brokerList);
// KafkaProducer 线程安全
        KafkaProducer<String, String> producer = new KafkaProducer<>
                (properties);
        ProducerRecord<String, String> record = new ProducerRecord<>(topic,
                "Kafka-demo-001", "hello, Kafka!");
        try {
            producer.send(record);
        } catch (Exception e) {
            e.printStackTrace();
        }
        producer.close();
    }
}